package med;

import java.util.Iterator;
import java.util.List;

import libsvm.svm_node;
import med.config.Config;

public class Vector {

	private List<String> words;
	private Format objectFormat;
	
	public String getWord(int index)
	{	
		if(index >= words.size())
			return null;
		return words.get(index);
		
	}
	
	Vector(List<String> i_words, Format i_objectFormat)
	{
		words = i_words;
		objectFormat = i_objectFormat;
	}
	
	private double wordToDouble(String str)
	{
		double code = 0;
		double a = 1;
		
		for(int i = str.length()-1; i>=0;i--)
		{
			code += str.getBytes()[i] * a;
			a*=SVMModule.mParam;
		}
		return code;
	}
	
	public double[] getDoubleVector()
	{
		double[] ret = new double[Config.getInstance().getTagPositionParam() + 1];
		Iterator<String> it = words.iterator();
		for (int i = 0; i<Config.getInstance().getTagPositionParam(); i++ ) {
			if(it.hasNext())
				ret[i] = wordToDouble(it.next());
		}
		ret[Config.getInstance().getTagPositionParam()] = objectFormat.ordinal()*100000;
		
	
		return ret;
		
	}
	public svm_node[] getSvmNode()
	{
		double[] doubleVector;
		doubleVector = getDoubleVector();
		svm_node[] svmNode = new svm_node[Config.getInstance().getTagPositionParam() + 2];
		
		int i = 0;
		for(double d : doubleVector)
		{
			svm_node temp = new svm_node();
			temp.index = i;
			temp.value = d;
			svmNode[i++] = temp;
		}
		svm_node svmEndNode = new svm_node();
		svmEndNode.index = -1;
		svmEndNode.value = -1;
		svmNode[Config.getInstance().getTagPositionParam()+1] = svmEndNode;
		return svmNode;
	}

	
	public List<String> getWords() {
		return words;
	}

	public void setWords(List<String> words) {
		this.words = words;
	}

	public Format getObjectFormat() {
		return objectFormat;
	}

	public void setObjectFormat(Format objectFormat) {
		this.objectFormat = objectFormat;
	}

	@Override
	public String toString() {
		return "{" + words.toString() + ":" + objectFormat +"}";
	}
}
